﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.UpdatePackageTool
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public static ContentControl contentControl;
        public static Dictionary<string, List<UserControl>> MainDictionary;
        public static UserControl selectdControl;
        public MainWindow()
        {
            InitializeComponent();
            contentControl = _ctlContentControl;
            MainDictionary = new Dictionary<string, List<UserControl>>();
            BingControls();
        }


        private void BingControls()
        {
            List<Product> products = XConfigUtil.Init(GetEnvironmentFilePath());
            if (products != null)
            {
                if (products != null)
                {
                    foreach(Product p in products)
                    {
                        List<UserControl> controls = new List<UserControl>();

                        BasicInfoControl basic  =   new BasicInfoControl(_ctlContentControl);

                        basic.DataContext = p;
                        controls.Add(basic);

                        OtherInfoControl other = new OtherInfoControl(_ctlContentControl, p);
                        controls.Add(other);
                        OperateControl operate = new OperateControl(_ctlContentControl, p);
                        controls.Add(operate);
                        MainDictionary.Add(p.AppType, controls);
                    }
                  
                    _ctlCombNames.ItemsSource = products;
                    _ctlCombNames.SelectedIndex = 0;
                }
            }
        }

        /// <summary>
        /// 获取Environment.xml文件的地址
        /// </summary>
        /// <returns></returns>
        public static string GetEnvironmentFilePath()
        {
            return string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, "Config\\ProductInfo.xml");
        }

        private void _ctlCombNames_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Product product = _ctlCombNames.SelectedItem as Product;
            List<System.Windows.Controls.UserControl> controls;
            MainDictionary.TryGetValue(product.AppType, out controls);

            if (selectdControl != null)
            {
                _ctlContentControl.Content = selectdControl;
            }
            else
            {
                _ctlContentControl.Content = controls[0];
            }
        }

    }
}
